package org.op.menu;

import java.io.File;

import javax.swing.ProgressMonitor;

import org.op.FrameView;
import org.op.nav.NavigationController;
import org.op.service.config.ConfigKeys;
import org.op.service.config.ConfigurationService;
import org.op.service.fileio.FileZipService;
import org.op.service.log.LoggerService;
import org.op.util.MessageController;

public class NotebookControllerImpl implements NotebookController {

	private FrameView frameView;
	private ConfigurationService configService;
	private NavigationController navController;
	private MessageController messageController;
	private FileZipService zipService;
	private LoggerService logger;

	public void newNotebook() {
		File f = openAndGetFolder();
		
		if (f!=null) {
			// conditions for a new notebook = f must be empty
			if (f.list().length>0) {
				frameView.showExceptionDialog("You can only create a new notebook in an empty directory.");
			} else {
				// set the workspace to this new folder
				String filePath = f.getAbsolutePath();
				updateWorkspace(filePath);
			}
		}
	}

	public void openNotebook() {
		File f = openAndGetFolder();
		
		// set the workspace to this new folder
		if (f!=null) {
			String filePath = f.getAbsolutePath();
			updateWorkspace(filePath);
		}
	}
	

	public void zipNotebook() {
		
		try {
			String workspaceFolderPath = configService.getApplicationProperties().getProperty(ConfigKeys.KEY_CURRENT_WORKSPACE);
			
			OpFileChooser fileChooser = new OpFileChooser(frameView.getFrame());
			fileChooser.setFromPath(workspaceFolderPath);
			fileChooser.setTitle(messageController.getMessage("enter.archive.path"));
			File file = fileChooser.openAndGet();
			if (file!=null) {
				String zipFilePath = file.getAbsolutePath();
				String zipMessage = messageController.getMessage("zip.in.progress");
				zipService.setInputPath(workspaceFolderPath);
				zipService.setOutputPath(zipFilePath);
				
				// use a Progress Monitor in case this is a long operation
				ProgressMonitor pm = new ProgressMonitor(null, zipMessage, "Initializing . . .", 0, 100);
				pm.setMillisToPopup(0);
				pm.setMillisToDecideToPopup(0);
				zipService.setMonitor(pm);
				
				Thread t = new Thread(zipService);  // SwingWorker??
				t.start();
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			frameView.showExceptionDialog(messageController.getMessage("zip.workspace.unsuccessful"));
		}
	}
	
	public void zipComplete() {
		frameView.showInfoDialog(messageController.getMessage("zip.successful"));
	}
	
	private File openAndGetFolder() {
		OpFileChooser fileChooser = new OpFileChooser(frameView.getFrame());
		fileChooser.setFoldersOnly(true);
		File f = fileChooser.openAndGet();
		
		return f;
	}
	
	private void updateWorkspace(String filePath) {
		configService.getApplicationProperties().setProperty(ConfigKeys.KEY_CURRENT_WORKSPACE, filePath);
		navController.loadNewTree();
	}

	public FrameView getFrameView() {
		return frameView;
	}

	public void setFrameView(FrameView frameView) {
		this.frameView = frameView;
	}

	public ConfigurationService getConfigService() {
		return configService;
	}

	public void setConfigService(ConfigurationService configService) {
		this.configService = configService;
	}

	public NavigationController getNavController() {
		return navController;
	}

	public void setNavController(NavigationController navController) {
		this.navController = navController;
	}

	public MessageController getMessageController() {
		return messageController;
	}

	public void setMessageController(MessageController messageController) {
		this.messageController = messageController;
	}

	public FileZipService getZipService() {
		return zipService;
	}

	public void setZipService(FileZipService zipService) {
		this.zipService = zipService;
	}

	public LoggerService getLogger() {
		return logger;
	}

	public void setLogger(LoggerService logger) {
		this.logger = logger;
	}
}
